package com.hxh.generater.springBoot.springboot3_vue3.gen;


import com.hxh.entity.Project;
import com.hxh.entity.TemplateConfig;
import com.hxh.entity.po.ClassInfo;
import com.hxh.generater.common.springboot3_vue3.Springboot3Vue3Config;
import com.hxh.generater.springBoot.genBase.GenTableCRUD;
import com.hxh.generater.springBoot.springboot3_vue3.gen.crud.*;
import com.hxh.generater.springBoot.util.GetAllTables;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class GenSb3Vue3TableCRUD implements GenTableCRUD {

    /**
     * @param project   :
     * @param classInfo :
     * @return void
     * @author HXH
     * @description: 生成实体类
     * @date 2024/5/21
     **/
    @Override
    public void generationEntity(Project project, ClassInfo classInfo) throws Exception {
        try {
            GenEntityRemote.generationOneEntity(project, classInfo);
        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成Entity异常！", e);
            throw e;
        }
    }

    /**
     * @param packageName
     * @param classInfo
     * @param templateConfig
     * @return
     * @throws Exception
     */
    @Override
    public String generationEntity(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        try {
            return GenEntityRemote.generationEntityClass(packageName, classInfo, templateConfig);
        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成Entity异常！", e);
            throw e;
        }
    }

    /**
     * @param packageName
     * @param classInfo
     * @param templateConfig
     * @return
     * @throws Exception
     */
    @Override
    public String generationEntityInput(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        try {
            return GenEntityRemote.generationEntityInputClass(packageName, classInfo, templateConfig);
        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成Entity异常！", e);
            throw e;
        }
    }

    /**
     * @param project   :
     * @param classInfo :
     * @return void
     * @author HXH
     * @description: 生成mybatis的mapper.xml
     * @date 2024/5/21
     **/
    @Override
    public void generationMapper(Project project, ClassInfo classInfo) throws Exception {
        try {
            GenMapperRemote.generationMapper(project, classInfo);
        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成Mapper异常！", e);
            throw e;
        }

    }

    /**
     * @param packageName
     * @param classInfo
     * @param templateConfig
     * @return
     * @throws Exception
     */
    @Override
    public String generationMapper(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        try {
            return GenMapperRemote.generationMapper(packageName, classInfo, templateConfig);
        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成Mapper异常！", e);
            throw e;
        }
    }

    /**
     * @param project   :
     * @param classInfo :
     * @return void
     * @author HXH
     * @description: 生成dao层接口类
     * @date 2024/5/21
     **/
    @Override
    public void generationDaoClass(Project project, ClassInfo classInfo) throws Exception {
        try {
            GenDaoRemote.generationDaoClass(project, classInfo);
        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成Dao异常！", e);
            throw e;
        }
    }

    /**
     * @param packageName
     * @param classInfo
     * @return
     * @throws Exception
     */
    @Override
    public String generationDaoClass(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        try {
            return GenDaoRemote.generationDaoClass(packageName, classInfo, templateConfig);
        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成Dao异常！", e);
            throw e;
        }
    }

    /**
     * @param project   :
     * @param classInfo :
     * @return void
     * @author HXH
     * @description: 生成service层代码
     * @date 2024/5/21
     **/
    @Override
    public void generationServiceClass(Project project, ClassInfo classInfo) throws Exception {
        try {
            GenServiceRemote.generationServiceClass(project, classInfo);
        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成Service异常！", e);
            throw e;
        }
    }

    /**
     * @param packageName
     * @param classInfo
     * @param templateConfig
     * @return
     * @throws Exception
     */
    @Override
    public String generationServiceClass(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        try {
            return GenServiceRemote.generationServiceClass(packageName, classInfo, templateConfig);
        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成Service异常！", e);
            throw e;
        }
    }

    @Override
    public void generationServiceImplClass(Project project, ClassInfo classInfo) throws Exception {
        try {
            GenServiceRemote.generationServiceImplClass(project, classInfo);
        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成ServiceImpl异常！", e);
            throw e;
        }
    }

    @Override
    public String generationServiceImplClass(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        try {
            return GenServiceRemote.generationServiceImplClass(packageName, classInfo, templateConfig);
        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成ServiceImpl异常！", e);
            throw e;
        }
    }

    /**
     * @param project   :
     * @param classInfo :
     * @return void
     * @author HXH
     * @description: 生成controller层代码
     * @date 2024/5/21
     **/
    @Override
    public void generationControllerClass(Project project, ClassInfo classInfo) throws Exception {
        try {
            GenControllerRemote.generationControllerClass(project, classInfo);
        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成Controller异常！", e);
            throw e;
        }
    }

    /**
     * @param packageName    : 包名
     * @param classInfo      :
     * @param templateConfig
     * @return void
     * @author HXH
     * @description:
     * @date 2024/5/23
     **/
    @Override
    public String generationControllerClass(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        try {
            return GenControllerRemote.generationControllerClass(packageName, classInfo, templateConfig);
        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成Controller异常！", e);
            throw e;
        }
    }

    /**
     * @param project :
     * @return void
     * @author HXH
     * @description: 生成全部表的crud
     * @date 2024/5/21
     **/
    @Override
    public void generationAllTableCRUD(Project project, boolean initExtendTable) throws Exception {
        try {
            ReplaceGroupIdUtil.replaceDirectory("controller", project);
            ReplaceGroupIdUtil.replaceDirectory("service", project);
            ReplaceGroupIdUtil.replaceDirectory("dao", project);
            ReplaceGroupIdUtil.replaceMapperXmlDirectory("resources\\mapper", project);

            List<ClassInfo> classInfos = new ArrayList<>();
            if (initExtendTable) {
                classInfos = GetAllTables.generationAllTableInfo(project, Springboot3Vue3Config.springboot3Vue3TableList);
            }
            for (ClassInfo classInfo : classInfos) {
                //生成字段映射文件
                generationEntity(project, classInfo);
                generationMapper(project, classInfo);
                generationDaoClass(project, classInfo);
                generationServiceImplClass(project, classInfo);
                generationServiceClass(project, classInfo);
                generationControllerClass(project, classInfo);
            }

        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成全部表的crud异常！", e);
            throw e;
        }
    }

    /**
     * @param project
     * @param classInfo
     */
    @Override
    public void generationEntityMappingClass(Project project, ClassInfo classInfo) throws Exception {
        try {
            GenEntityRemote.generationOneEntityMappingClass(project, classInfo);
        } catch (Exception e) {
            log.error("springboot3_vue3模板：生成EntityMapping异常！", e);
            throw e;
        }

    }

}
